#!/bin/env python


from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import datetime  # For datetime objects

import backtrader as bt

# Create a Stratey
class TestStrategy(bt.Strategy):
    def start(self):
        # 最开始时调用
        print('start')
    
    def prenext(self):
        # 当均线等值没有到位时，每天调用
        print('prenext')
    
    def nextstart(self):
        # 当均线等值刚到位时，调用一次
        print('nextstart')
    
    def next(self):
        # 每天调用
        print('next')
        
    def stop(self):
        # 结束时调用
        print('stop')
    
if __name__ == '__main__':
    cerebro = bt.Cerebro()
    
    cerebro.addstrategy(TestStrategy)
    
    # Create a Data Feed
    data = bt.feeds.YahooFinanceCSVData(
        dataname='orcl-1995-2014.txt',
        # Do not pass values before this date
        fromdate=datetime.datetime(2000, 1, 1),
        # Do not pass values after this date
        todate=datetime.datetime(2000, 12, 31),
        reverse=False)

    # Add the Data Feed to Cerebro
    cerebro.adddata(data)

    # Set our desired cash start
    cerebro.broker.setcash(100000.0)
    
    cerebro.addsizer(bt.sizers.FixedSize, stake=1000)

    cerebro.broker.setcommission(commission=0.001)

    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())

    cerebro.run()

    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

    cerebro.plot()

